Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DOMAIN_NAME for networks #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Jean-Beru
Copy link
Contributor

Resolves #32

Add the possibility to add a DOMAIN_NAME for containers in a network. To specify the network concerned by the domain name, add my_network_name: before value. Exemple :

version: '2'

networks:
  first:
    driver: bridge
  second:
    driver: bridge

services:
  web:
    image: php:7-apache
    environment:
      - "DOMAIN_NAME=first:web,first:www.web"
    networks:
      - first
      - second
  db:
    image: php:7-apache
    environment:
      - "DOMAIN_NAME=first:db,second:database"
    networks:
      - first
      - second
  mail:
    image: php:7-apache
    environment:
      - "DOMAIN_NAME=first:mail"
    networks:
      - first

will result in

## docker-hostmanager-start
172.26.0.4 ... db.dockerhostmanager_first db
172.25.0.3 ... db.dockerhostmanager_second database
172.26.0.3 ... web.dockerhostmanager_first web www.web
172.25.0.2 ... web.dockerhostmanager_second
172.26.0.2 ... mail.dockerhostmanager_first mail
## docker-hostmanager-end

@@ -126,6 +126,8 @@ private function getHostsLines($container)
// Networks
if (isset($container['NetworkSettings']['Networks']) && is_array($container['NetworkSettings']['Networks'])) {
foreach ($container['NetworkSettings']['Networks'] as $networkName => $conf) {
preg_match(sprintf('/^%s_(.+)$/', $this->getProjectName($container)), $networkName, $matches);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it working with a container not created by docker compose ?
docker run ...

@egeloen
Copy link

egeloen commented Feb 6, 2017

@iamluc What the state of this PR? I would love to see it merged :)

@iamluc
Copy link
Owner

iamluc commented Feb 7, 2017

ping @Jean-Beru
Have you time to work on this ?

@Jean-Beru
Copy link
Contributor Author

Not really but I will do an effort just fo you @iamluc ;-)

@leokun
Copy link

leokun commented Feb 2, 2018

Hi there,
Is there any news about this PR ?

mykiwi and others added 3 commits April 21, 2018 11:18
./bin/docker-hostmanager
PHP Fatal error:  Uncaught Error: Call to undefined method DockerHostManager\Synchronizer::getAdditionalHosts() in src/Synchronizer.php:181
@dkarlovi
Copy link

dkarlovi commented Apr 27, 2018

Note: it seems that using this patch does work, but you need to specify the network, like so:

  web:
    environment:
      - DOMAIN_NAME=default:foo.bar

Without the default, it did not work for me. Maybe we can allow for default being equal to null so it works out of the box?

@dkarlovi
Copy link

@iamluc what can we do to get this built and pushed to Docker Hub? :)

@iamluc
Copy link
Owner

iamluc commented Apr 27, 2018

Hi @dkarlovi thanks for testing.

I'm gonna try to finish this PR with @Jean-Beru and @mykiwi in a few days.

@@ -126,6 +126,8 @@ private function getHostsLines($container)
// Networks
if (isset($container['NetworkSettings']['Networks']) && is_array($container['NetworkSettings']['Networks'])) {
foreach ($container['NetworkSettings']['Networks'] as $networkName => $conf) {
preg_match(sprintf('/^%s_(.+)$/', $this->getProjectName($container)), $networkName, $matches);
$networkShortName = isset($matches[1]) ? $matches[1] : null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                $networkShortName = isset($matches[1]) ? $matches[1] : 'default';

should allow to not require setting the name as DOMAIN_NAME=default:foo.bar if the default network is used.

@dkarlovi
Copy link

dkarlovi commented May 1, 2018

BTW I've pushed this to https://hub.docker.com/r/dkarlovi/docker-hostmanager/ as a workaround, will remove it once this gets merged.

@Wirone
Copy link

Wirone commented Jun 3, 2018

@iamluc any news on this?

@iamluc
Copy link
Owner

iamluc commented Jun 11, 2018

I discussed with @Jean-Beru about this PR, and the need is not clearly defined (IMHO).

Could you tell if those examples help you to do what you want to achieve ?
https://github.com/iamluc/docker-hostmanager/pull/39/files

Any help to improve the documentation is also welcomed 😄

@Wirone
Copy link

Wirone commented Jun 11, 2018

@iamluc hostmanager adds some non-human-friendly Docker hosts to /etc/hosts:

## docker-hostmanager-start
172.20.0.2 reverse-proxy.traefik_loc 5b13bd735a62.traefik_loc traefik.traefik_loc
172.27.0.3 rabbitmq.arm_default 6f60c1c9caa7.arm_default arm_rabbitmq_1.arm_default
## docker-hostmanager-end

The goal here is to allow define user-friendly one(s) so it can be accessed and configured as developer wants, so instead of the lines above we can get:

## docker-hostmanager-start
172.20.0.2 traefik.loc reverse-proxy.traefik_loc 5b13bd735a62.traefik_loc traefik.traefik_loc
172.27.0.3 rabbit.loc rabbitmq.arm_default 6f60c1c9caa7.arm_default arm_rabbitmq_1.arm_default
## docker-hostmanager-end

and use traefik.loc and rabbit.loc in the browser or other tools (always, even if project's directories and/or services would change name).

@iamluc
Copy link
Owner

iamluc commented Jun 12, 2018

@Wirone
What I mean is that you can already do that:

version: '2'

networks:
    default:
        external:
            name: loc

services:
    traefik:
        image: traefik

    rabbit:
        image: rabbit

Will have the same result.

Of course you need to create the loc network yourself.
But it can be automated. I personally do it in a Makefile (e.g. make start)

@dkarlovi
Copy link

@iamluc

Of course you need to create the loc network yourself.

This makes the process a bit more complex in a cross-cutting team, as you must always ship additional tooling too. It might also lead to collisions (where multiple projects are trying to create the same external network).

Also, it doesn't allow you to have different / multiple / no suffixes if the need arises, if I understood correctly.

@iamluc
Copy link
Owner

iamluc commented Jun 12, 2018

This makes the process a bit more complex in a cross-cutting team, as you must always ship additional tooling too

In fact, this is not needed anymore. Since compose file version 3.5, you can change the name of the network (https://docs.docker.com/compose/compose-file/#name-1).

networks:
    default:
        name: mynetwork

Also, it doesn't allow you to have different / multiple / no suffixes

You can have define multiple networks, it will result to multiple suffix.

version: '3.5'

networks:
    default:
        name: mynetwork
    backend:
        name: backend

services:
    app:
        image: myimage
        networks:
            - default
            - backend

Indeed a suffix is required, but I don't think it is a good idea to not have one.

A very important point to me is that with this system, hostnames are the same whether you are inside a container or outside from you host, because we do the same thing docker does.

If we allow changing the hostname with a label or an env var, it will works ONLY from the host

@dkarlovi
Copy link

hostnames are the same whether you are inside a container or outside from you host, because we do the same thing docker does

That does sound neat. But, would it lead to problems?

For example, I wish to always use the suffix local for my projects. If I start up two separate projects at the same time, they'll both try to create redis.local even though I don't need Redis to be available at all.

Also, note that I'm using a actual valid FQDN for some of my projects to allow them to have valid Let's Encrypt certs (example, project.dev.company.com). This would mean my network name would have to be a very common TLD.

@dkarlovi
Copy link

Hi @iamluc, have you decided how to proceed here?

@iamluc
Copy link
Owner

iamluc commented Aug 1, 2018

IMHO, this feature is not needed so I will not work on it.

BUT, if someone wants to finish it, validates it is working and ensures it breaks nothing. I will agree to merge it.

@dkarlovi
Copy link

dkarlovi commented Aug 1, 2018

We've been using this since I've pushed my own image with the PR merged. It does work as expected.

@iamluc
Copy link
Owner

iamluc commented Aug 1, 2018

Yep, but it would be great if someone could reply to the comments:
https://github.com/iamluc/docker-hostmanager/pull/34/files#r86107703
https://github.com/iamluc/docker-hostmanager/pull/34/files#r184656291

And also document this feature in the README

@dkarlovi
Copy link

dkarlovi commented Aug 1, 2018

Fair 👍 I try to get this done in the near future.

@iamluc
Copy link
Owner

iamluc commented Aug 1, 2018

also, please change the target of the PR to the develop branch. This way I will test it for some days.

@@ -136,6 +138,12 @@ private function getHostsLines($container)
$hosts[] = $alias.'.'.$networkName;
}

foreach ($this->getAdditionalContainerHosts($container) as $host) {
if (preg_match('/^(.+):(.+)$/', $host, $matches) && $matches[1] === $networkShortName) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this check and not directly add the host in the array ?

{
$hosts = [];

$domainName = $this->getEnv($container, 'DOMAIN_NAME');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to suggest that DOMAIN_NAME is not entirely correct name for what we want to achieve with this feature. "Domain" is rather HTTP concept, but host manager can be used also for database containers instead of port binding (so developer can connect to database with client by custom host, not by localhost with bound port).

@Wirone
Copy link

Wirone commented Feb 26, 2019

@dkarlovi @iamluc would be good to finish this somehow... is there something we can do to get this PR merged? Using Dalibor's fork is OK, but maintaining it (keeping up-to-date) isn't something easy I think. Additionally, Dalibor's fork does NOT contain changes in readme, so sharing link to the repo isn't enough (I just sent it to my colleague and he installed iamluc/docker-hostmanager because documentation states so...).

@dkarlovi
Copy link

Gotta admit I've forgotten about my promise here. I'm using my image ever since I've pushed it, with my entire team (we've documented it in our project) so it's basically fire & forget currently.

@Wirone If you feel like you'd get additional value from this being merged, maybe you can take over the PR and get it merged?

@Wirone
Copy link

Wirone commented Feb 26, 2019

@dkarlovi how is it possible technically? PR is from @Jean-Beru's branch, if we want to continue in this PR I would need to get access to that repo 🤔? Or maybe you mean forking fork and creating new PR?

@dkarlovi
Copy link

You just fork this repo, add @Jean-Beru's repo as an additional remote and checkout the PR branch into your own. Any commit you make would of course be made to your own branch which gets a separate PR, but all the code is already there. Otherwise you'd need to do PRs against that repo which is inefficient.

There's some Github magic with refs which allows you to do it automagically, but I always forget how.

@jlefebvre1997
Copy link

Yo, do we have to drop this one in favor of dkarlovi's one if this feature does not workas expected ? 😮

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docker-compose - The domain name was not added to hosts file
8 participants